package com.xxx.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xxx.config.Request;
import com.xxx.pojo.*;
import com.xxx.pojo.vo.ProductVo;
import com.xxx.service.ProductService;
import com.xxx.utli.UploadOss;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author George
 * @since 2023-07-06
 */
@Api(tags = "产品控制")
@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    /**
     * 连表查询产品信息
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据条件查询产品列表", notes = "根据分页查询参数查询产品列表")
    @PostMapping("/productsListByCondition")
    public Request getProductsListByCondition(
            @RequestParam("searchValue") String searchValue,
            @RequestParam("pageNum") int pageNum,
            @RequestParam("pageSize") int pageSize) {

        Page<ProductVo> mapPage = new Page<>(pageNum, pageSize, true);


        // 去除查询条件中的空格
        String finalSearchValue = StringUtils.isNotBlank(searchValue) ? searchValue.trim() : null;

        MPJLambdaWrapper<Product> lambdaWrapper = new MPJLambdaWrapper<Product>()
                .selectAll(Product.class)
                .selectAll(ProductDetails.class)
                .leftJoin(ProductDetails.class, ProductDetails::getProductId, Product::getProductId);

        if (StringUtils.isNotBlank(finalSearchValue)) {
            lambdaWrapper.and(wrapper -> wrapper
//                    根据商品名称查询
                    .like(Product::getProductName, finalSearchValue)
                    .or()
//                    根据商品品牌查询
                    .like(Product::getProductBrand, finalSearchValue))
                    .or()
//                    根据产品类型查询
                    .like(Product::getProductTypeId, finalSearchValue);
        }

        productService.selectJoinListPage(mapPage,ProductVo.class,lambdaWrapper);
        List<ProductVo> records = mapPage.getRecords();
        long total = mapPage.getTotal();

        return records.size() > 0 ? Request.suc(total,records) : Request.error("查询产品失败");
    }


    /**
     * 连查询产品全部信息
     *
     * @return 返回用户列表和总数
     */
    @ApiOperation(value = "根据产品编号查询产品详情", notes = "根据产品编号查询用户产品信息")
    @PostMapping("/productByWorkNumber")
    public Request getProductByWorkNumber(@RequestParam("productId") String productId) {
        List<ProductVo> userVoList = productService.selectJoinList(ProductVo.class,
                new MPJLambdaWrapper<Product>()
                        .selectAll(Product.class)
                        .selectAll(ProductDetails.class)
                        .leftJoin(ProductDetails.class, ProductDetails::getProductId, Product::getProductId)
                        .eq(Product::getProductId, productId)
        );
        return userVoList.size() > 0 ? Request.suc(userVoList) : Request.error("根据工号查询产品详情失败");
    }


    /**
     * 根据根据产品类型编号查询用户
     *
     * @return 返回符合条件的用户列表
     */
    @ApiOperation(value = "根据产品类型分类查询", notes = "根据产品类型分类查询")
    @PostMapping("/productsByDepartment")
    public Request getProductsByDepartment(@RequestParam("typeId") String typeId) {
        List<ProductVo> productVoList = productService.selectJoinList(ProductVo.class,
                new MPJLambdaWrapper<Product>()
                        .selectAll(Product.class)
                        .selectAll(Type.class)
                        .leftJoin(Type.class, Type::getTypeId, Product::getProductTypeId)
                        .eq(Type::getTypeId, typeId)
        );
        return productVoList.size() > 0 ? Request.suc(productVoList) : Request.error("根据产品编号分类查询失败");
    }


    /**
     * 批量下架商品
     *
     * @return 返回操作结果
     */
    @ApiOperation(value = "批量下架商品", notes = "根据产品编号批量下架商品户")
    @DeleteMapping("/disableProducts")
    public Request disableProductsByIds(@RequestParam("products") List<Long> products) {
        boolean disabled = productService.removeByIds(products);
        return disabled ? Request.suc() : Request.error("批量下架产品");
    }


    /**
     * 添加商品
     *
     * @param product 商品对象
     * @return 返回操作结果
     */
    @ApiOperation(value = "添加商品", notes = "添加商品")
    @PostMapping("/disableProducts")
    public Request productSave(@ModelAttribute  Product product){
        boolean save = productService.save(product);
        return save? Request.suc("添加成功") : Request.error("添加失败");
    }

    /**
     * 上传图片并返回图片路径
     *
     * @param file 要上传的图片文件
     * @return 图片路径
     */
    @PostMapping("/upImg")
    private String upImg(MultipartFile file) throws IOException {
        String Url = UploadOss.uploadImage(file);
        System.out.println(Url);
        return Url;
    }

}

